<?php

class ReportsController extends CController
{
    public function actionUsersReport()
    {
        $model = new User;
        $class = get_class($model);

        if (isset($_GET[$class])) {
            $model->setAttributes($_GET[$class], false);

            if (isset($_GET['generateReport']) && $_GET['generateReport'] == 1) {
                $report = Yii::app()->csvexporter->buildReport($model->search(), array(
                    'id',
                    'first_name',
                    'last_name' => array('header' => 'Test custom header'),
                    // 'rating'    => array('relation' => 'rating')
                ));

                // $report->onlyIncluded = true;

                Yii::app()->request->sendFile('usersReport_' . date('Y-m-d')
                    . '.csv', $report->asText());
                // $report->asFile(Yii::app()->params->reportsPath, 'testtesttest.csv');
            }
        }

        $this->render('users_list', compact('model'));
    }

    public function actionCreateUser()
    {
        $model = new User;
        $class = get_class($model);

        $model->first_name = 'test first name 1';
        $model->last_name = 'test last name 1';
        $model->email = 'test1@gmail.com';
        $model->phone = '0998887744';
        $model->social_network = 'facebook';

        $model->save();
    }

    public function actionUpdateUser()
    {
        $model = User::model()->findByPk(11);

        $model->first_name = 'test first name 2';
        $model->last_name = 'test last name 2';
        $model->email = 'test2@gmail.com';
        $model->phone = '0998887744';
        $model->social_network = 'facebook';

        $model->save();
    }

    public function actionDeleteUser()
    {
        User::model()->deleteByPk(11);
    }

    public function actionRestoreUser()
    {
        $log = UserActivity::model()->findByPk(2);

        $user = new User;
        $user->attributes = unserialize($log->previous_data);
        $user->save();
    }
}
